package com.xxrl.shop.domain;

import com.google.gson.Gson;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LinuY
 * @date 2020/7/27 14:42
 */
@Data
@Entity
@NoArgsConstructor
@Table(name = "sp_log")
@EntityListeners(AuditingEntityListener.class)
public class Log implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String userName;

    private String className;
    private Integer type;
    private String url;
    private String requestType;
    private Long createDate;

    private Long logElapsedTime;

    private String requestParam;

    @CreatedDate
    private Long createTime;

    @LastModifiedDate
    private Long lastModifiedTime;

    /**
     * 设置请求参数
     *
     * @param paramMap paramMap
     */
    public void setMapToParams(Map<String, String[]> paramMap) {
        if (paramMap == null) {
            return;
        }
        Map<String, Object> params = new HashMap<>();
        for (Map.Entry<String, String[]> param : paramMap.entrySet()) {

            String key = param.getKey();
            String paramValue = (param.getValue() != null && param.getValue().length > 0 ? param.getValue()[0] : "");
            String obj = StringUtils.endsWithIgnoreCase(param.getKey(), "password") ? "你是看不见我的" : paramValue;
            params.put(key,obj);
        }
        this.requestParam = new Gson().toJson(params);
    }

}
